package array

// 将m*n的mat矩阵重置为r*c的矩阵
// 如果不合理则返回原矩阵

func MatrixReshape(mat [][]int, r int, c int) [][]int {
	m, n := len(mat), len(mat[0])

	// 所谓不合理为m*n是否等于r*c
	if m*n != r*c {
		return mat
	}

	res := make([][]int, r)

	nums := []int{}
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			nums = append(nums, mat[i][j])
		}
	}

	index := 0

	for i := 0; i < r; i++ {
		res[i] = make([]int, 0)
		for j := 0; j < c; j++ {
			res[i] = append(res[i], nums[index])
			index++
		}
	}

	return res

}
